package com.app.conrtoller;

import com.alibaba.fastjson.JSON;
import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.hsy.*;
import com.app.domain.shhy.HyResultJson;
import com.app.domain.shhy.PostGroutData;
import com.app.domain.shhy.PostMultiGroutSSProcess;
import com.app.service.SendMessageService;
import com.app.service.UploadParameterService;
import com.app.service.UploadService;
import com.app.service.YjDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 上海华岩
 */
@Slf4j
@RestController
@RequestMapping("/shhy")
public class ShhyUploadController {
    @Value("${yj.shhy.serviceHost}")
    private String serviceHost;
    @Value("${yj.shhy.processHost}")
    private String processHost;
    @Value("${yj.shhy.projectId}")
    private String projectId;
    @Value("${yj.shhy.sectionCode}")
    private String sectionCode;
    @Value("${yj.shhy.factoryCode}")
    private String factoryCode;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    UploadService uploadService;
    @Autowired
    SendMessageService sendMessageService;


    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
        for (String id : ids) {
            YjData yjData = yjDataService.findOne(id);
            UploadParameter uploadParameter = uploadParameterService.findOne("7");
            PostGroutData postGroutData = uploadService.getPostGroutData(yjData, uploadParameter, projectId, sectionCode, factoryCode);
            String s = JSON.toJSONString(postGroutData);
            HyResultJson hyResultJson = uploadService.sendShhyMessage(serviceHost, s);
            if (hyResultJson.getSuccess()) {
                PostMultiGroutSSProcess process = uploadService.getPostMultiGroutSSProcess(yjData, uploadParameter, projectId, sectionCode, factoryCode);
                String s1 = JSON.toJSONString(process);
                HyResultJson json = uploadService.sendShhyMessage(processHost, s1);
                if (json.getSuccess()) {
                    updateState("1", String.valueOf(yjData.getId()));
                    succTotal++;
                } else {
                    updateState("0", String.valueOf(yjData.getId()));
                    failTotal++;
                }
            } else {
                updateState("0", String.valueOf(yjData.getId()));
                failTotal++;
            }

//            String uuid = sendMessageService.ConditionQuery(id);
//            if (StringUtils.isEmpty(uuid)) {
//                List<Yjsy_M> yjsy_m = uploadService.getYjsy_M(yjData, uploadParameter);
//                ResponseResult responseResult = uploadService.sendHsyMessage("SWYJSY_M", serviceHost, yjsy_m);
//                if ((responseResult != null) && (responseResult.getSendCount().equals(responseResult.getSuccCount()))) {
//                    //主表上传成功
//                    SendMessage sendMessage = new SendMessage();
//                    sendMessage.setLh(yjData.getBridge_code());
//                    sendMessage.setKh(yjData.getDuct_code());
//                    sendMessage.setBusi_type("SWYJSY_M");
//                    sendMessage.setUpload_time(new Date());
//                    sendMessage.setUuid(yjsy_m.get(0).getSyjid());
//                    sendMessage.setStatus("0");
//                    sendMessageService.insert(sendMessage);
//                    List<Yjsy_S> yjsy_sList = uploadService.getYjsy_s(yjData, uploadParameter, yjsy_m.get(0).getSyjid());
//                    ResponseResult responseResult1 = uploadService.sendHsyMessage("SWYJSY_S", serviceHost, yjsy_sList);
//                    if ((responseResult1 != null) && (responseResult1.getSendCount().equals(responseResult1.getSuccCount()))) {
//                        List<Yjsy_S_S> yjsy_s_s = uploadService.getYjsy_S_S(yjData, uploadParameter, yjsy_sList.get(0).getHoleid());
//                        ResponseResult responseResult2 = uploadService.sendHsyMessage("SWYJSY_S_S", serviceHost, yjsy_s_s);
//                        if ((responseResult2 != null) && (responseResult2.getSendCount().equals(responseResult2.getSuccCount()))) {
//                            updateState("1", String.valueOf(yjData.getId()));
//                            succTotal++;
//                        } else {
//                            log.info("-------------压浆记录SWYJSY_S_S数据发送失败------------------------");
//                            updateState("0", String.valueOf(yjData.getId()));
//                            failTotal++;
//                        }
//                    } else {
//                        log.info("-------------压浆记录SWYJSY_S数据发送失败------------------------");
//                        updateState("0", String.valueOf(yjData.getId()));
//                        failTotal++;
//                    }
//                } else {
//                    log.info("-------------压浆记录SWYJSY_M数据发送失败------------------------");
//                    updateState("0", String.valueOf(yjData.getId()));
//                    failTotal++;
//                }
//            } else {
//                List<Yjsy_S> yjsy_sList = uploadService.getYjsy_s(yjData, uploadParameter, uuid);
//                ResponseResult responseResult1 = uploadService.sendHsyMessage("SWYJSY_S", serviceHost, yjsy_sList);
//                if ((responseResult1 != null) && (responseResult1.getSendCount().equals(responseResult1.getSuccCount()))) {
//                    List<Yjsy_S_S> yjsy_s_s = uploadService.getYjsy_S_S(yjData, uploadParameter, yjsy_sList.get(0).getHoleid());
//                    ResponseResult responseResult2 = uploadService.sendHsyMessage("SWYJSY_S_S", serviceHost, yjsy_s_s);
//                    if ((responseResult2 != null) && (responseResult2.getSendCount().equals(responseResult2.getSuccCount()))) {
//                        updateState("1", String.valueOf(yjData.getId()));
//                        succTotal++;
//                    } else {
//                        log.info("-------------压浆记录SWYJSY_S_S数据发送失败------------------------");
//                        updateState("0", String.valueOf(yjData.getId()));
//                        failTotal++;
//                    }
//                } else {
//                    log.info("-------------压浆记录SWYJSY_S数据发送失败------------------------");
//                    updateState("0", String.valueOf(yjData.getId()));
//                    failTotal++;
//                }
//            }
        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }


    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }
}
